package com.web.jdbcbase.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.web.jdbcbase.dao.MenuDao;
import com.web.jdbcbase.module.Menu;
import com.web.jdbcbase.service.MenuService;
import com.web.jdbcyw1.dao.UserDao;
import com.web.jdbcyw1.module.User;
import com.web.jdbcyw1.service.UserService;
@Service("menuService")
public class MenuServiceImpl implements MenuService {
	@Resource
	private MenuDao menuDao;
	@Resource//注入url数据源sevice
	private UserService userService;
	@Resource
	private UserDao userDao;
	@Override
	public List<Menu> queryMenu(Menu menu) {
		return this.menuDao.queryMenu(menu);
	}
	@Override
	public Menu queryMenuForOne(String str) {
		return this.menuDao.queryMenuForOne(str);
	}
	@Override
	public List<Map> queryMenuMap(Menu menu) {
		return this.menuDao.queryMenuMap(menu);
	}
	@Override
	@Transactional("transactionManager2")
	public int addMenu(Menu menu) {
		System.out.println(2+"===========");
		int flag = this.menuDao.addMenu(menu);
		System.out.println(1/0);
		return flag;
	}
	/////////////////////////////////////////第二个数据源test
	@Override
	public List<User> queryUser() {
		User u = new User();
		return this.userDao.queryUser(u);
	}
	@Override
	@Transactional("transactionManager")
	public int addUser(User user) {
		System.out.println("tttttttttt");
		int flag = this.userDao.addUser(user);
		//System.out.println(1/0);
		return flag;
	}
	@Override
	@Transactional
	public int addUserAndMenu(User user,Menu menu){
		//new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		ApplicationContext ack2 = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		SqlSessionFactory sqlSessionFactory= (SqlSessionFactory) ack2.getBean("sqlSessionFactory");
//		SqlSession ss = sqlSessionFactory.openSession();
		//List<User> list = ss.selectList("queryUser");
		//ss.insert("addUser", user);
		//ss.commit(true);
		//System.out.println(list+"===++++++====");
		System.out.println("atttttttttt");
		int flag = 0;
		flag += this.menuDao.addMenu(menu);
//		flag += this.userDao.addUser(user);
		flag += test(user);
		//ss.rollback(true);
//		System.out.println(1/0);
		return flag;
	}
//	@Transactional
	public int test(User user){
		int flag = this.userDao.addUser(user);
		System.out.println(1/0);
		return flag;
	}
}
